package xg.glycomaid;



import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
import javax.swing.border.EmptyBorder;

import xg.glycomaid.utils.TextUtils;

public class ProgressDialog extends JDialog
    implements ActionListener
{

    /**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	public ProgressDialog(JFrame parent, String message, int min, int max)
    {
        super(parent, message, false);
        is_canceled = false;
        getContentPane().setLayout(new BoxLayout(getContentPane(), 1));
//        theMessage = new JLabel(message);
//        theMessage.setBorder(new EmptyBorder(10, 20, 10, 0));
//        getContentPane().add(theMessage);
//        theNote = new JLabel(note);
//        theNote.setBorder(new EmptyBorder(0, 10, 10, 10));
//        getContentPane().add(theNote);
        if(min == -1 || max == -1)
        {
            theBar = new JProgressBar();
            theBar.setBorder(new EmptyBorder(0, 5, 10, 5));
            theBar.setIndeterminate(true);
        } else
        {
            theBar = new JProgressBar(min, max);
        }
        getContentPane().add(theBar);
        JPanel buttons_panel = new JPanel(new FlowLayout());
        cancel_button = new JButton("Cancel");
        cancel_button.addActionListener(this);
        buttons_panel.add(cancel_button);
        getContentPane().add(buttons_panel);
        pack();
        setResizable(false);
        setLocationRelativeTo(parent);
    }

    public boolean isCanceled()
    {
        return is_canceled;
    }

    public void setNote(String note)
    {
        theNote.setText(note);
        pack();
    }

    public void setValue(int val)
    {
        theBar.setValue(val);
    }

    public void actionPerformed(ActionEvent e)
    {

        String str = e.getActionCommand();
        String action;
        int ind = TextUtils.find(str, '=');
        if(ind == -1)
        	 action=str;
        else
        	action= str.substring(0, ind);
    	
        if(action.equals("Cancel"))
        {
            is_canceled = true;
            setVisible(false);
        }
    }

    private boolean is_canceled;
    private JLabel theMessage;
    private JLabel theNote;
    private JProgressBar theBar;
    private JButton cancel_button;
}






